//
//  PictureBrowse.m
//  DSCL
//
//  Created by QSC on 2018/5/22.
//  Copyright © 2018年 macbook. All rights reserved.
//

#import "PictureBrowse.h"
#import "PictureCollectionCell.h"
@interface PictureBrowse()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic,strong) UILabel *topLabel;

@property (nonatomic,strong) UICollectionView *collectionView;
@end
@implementation PictureBrowse

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        
        
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
        layout.itemSize = CGSizeMake(ScreenW, ScreenH);
        
        layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
        layout.minimumInteritemSpacing = 0;
        layout.minimumLineSpacing = 0;
        //水平滚动
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        
        UICollectionView *collectionView = [[UICollectionView alloc]initWithFrame:self.bounds collectionViewLayout:layout];
        
        collectionView.backgroundColor = [UIColor colorWithWhite:0.f alpha:0.9];
        collectionView.dataSource = self;
        collectionView.delegate = self;
        
        [collectionView registerClass:[PictureCollectionCell class] forCellWithReuseIdentifier:@"PictureCollectionCell"];
        // 去掉滚动条
        collectionView.showsHorizontalScrollIndicator = NO;
        // 分页
        collectionView.pagingEnabled = YES;
        collectionView.bounces = NO;
        self.collectionView = collectionView;
        
        [self addSubview:collectionView];
        
        
        //上面显示页数的label
        UILabel *label = [[UILabel alloc]init];
        label.textColor = [UIColor whiteColor];
        label.textAlignment = NSTextAlignmentCenter;
        self.topLabel = label;
        [self addSubview:label];
        

        
        
        
    }
    return self;
}
-(void)layoutSubviews
{
    [super layoutSubviews];
    
    self.collectionView.frame = self.bounds;
    CGFloat offset = ScreenW * (_index - 1);
    self.collectionView.contentOffset = CGPointMake(offset, 0);
    
    
    self.topLabel.top = 30;
    
    self.topLabel.width = 50;
    self.topLabel.height = 20;
    self.topLabel.centerX = self.centerX;
    self.topLabel.text = [NSString stringWithFormat:@"%lu/%lu",(unsigned long)self.index,(unsigned long)self.imageArr.count];
    
    
    
}

#pragma - dataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    
    return _imageArr.count;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *) collectionView cellForItemAtIndexPath:(nonnull NSIndexPath *)indexPath
{
    PictureCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"PictureCollectionCell" forIndexPath:indexPath];
    
    cell.imageSource = _imageArr[indexPath.item];
    
    WeakSelf
    cell.tapBlock = ^(){
        //点击图片退出
        [UIView animateWithDuration:0.3 animations:^{
            weakSelf.alpha = 0.0;
        } completion:^(BOOL finished) {
            [weakSelf removeFromSuperview];
        }];
    };
    
    return cell;
}



// 监听滚动
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    int index = scrollView.contentOffset.x/scrollView.width;
    self.topLabel.text = [NSString stringWithFormat:@"%lu/%lu",(unsigned long)index + 1,(unsigned long)self.imageArr.count];
    
    
    
}

@end
